package com.ytjj.qmyx.admin.controller.imController;

import com.ytjj.common.api.CommonResult;
import com.ytjj.common.exception.ApiException;
import com.ytjj.qmyx.admin.annotation.OperLog;
import com.ytjj.qmyx.admin.constants.SysLogConstants;
import com.ytjj.qmyx.admin.eunm.im.ImRepositoryEnums;
import com.ytjj.qmyx.admin.model.ImIssueDescribe;
import com.ytjj.qmyx.admin.model.request.ImIssueDescribeReq;
import com.ytjj.qmyx.admin.model.response.im.ImIssueDescribeResponse;
import com.ytjj.qmyx.admin.service.imService.ImIssueDesscribeService;
import com.ytjj.qmyx.admin.utils.EmptyUtil;
import com.ytjj.qmyx.admin.utils.UserUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

/**
 *
 * 问题描述
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/issueDescribe")
public class ImIssueDescribeController {

    private ImIssueDesscribeService imIssueDesscribeService;

    public CommonResult Describe(ImIssueDescribeResponse imIssueDescribeResponse){
        if(EmptyUtil.isEmpty(imIssueDescribeResponse.getStandards())){
            throw new ApiException(ImRepositoryEnums.ISSUE_STANDARD_IS_NULL.getMessage());
        }
        if(EmptyUtil.isEmpty(imIssueDescribeResponse.getAnswer())){
            throw new ApiException(ImRepositoryEnums.ISSUE_ANSWER_IS_NULL.getMessage());
        }
        return CommonResult.success();
    }

    @PostMapping("/describeList")
    @OperLog(operModule = "问题描述", operDesc = "问题描述列表", operType = SysLogConstants.OPERATION_SELECT)
    public CommonResult describeList(@RequestBody ImIssueDescribeReq imIssueDescribeReq) {
        if(EmptyUtil.isEmpty(imIssueDescribeReq.getClassifyId())||imIssueDescribeReq.getClassifyId()==0){
            return CommonResult.failed(ImRepositoryEnums.ISSUE_CLASSIFY_IS_NULL.getMessage());
        }
        return CommonResult.success(imIssueDesscribeService.describeList(imIssueDescribeReq));
    }

    @PostMapping(value = "/insertDescribe")
    @OperLog(operModule = "问题描述", operDesc = "新增问题描述", operType = SysLogConstants.OPERATION_ADD)
    public CommonResult insertDescribe(@RequestBody ImIssueDescribeResponse imIssueDescribeResponse){
        Describe(imIssueDescribeResponse);
        if(EmptyUtil.isEmpty(imIssueDescribeResponse.getClassifyId())){
            throw new ApiException(ImRepositoryEnums.ISSUE_CLASSIFY_IS_NULL.getMessage());
        }
        imIssueDescribeResponse.setCreateName(UserUtil.getAdminId());
        return CommonResult.success(imIssueDesscribeService.insertDescribe(imIssueDescribeResponse));
    }

    @GetMapping(value = "/deleteDescribe")
    @OperLog(operModule = "问题描述", operDesc = "移除问题描述", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult deleteDescribe(Integer id){
        if(EmptyUtil.isEmpty(id)){
            throw new ApiException(ImRepositoryEnums.ISSUE_DESCRIBE_IS_NULL.getMessage());
        }
        return CommonResult.success(imIssueDesscribeService.deleteDescribe(id));
    }

    @PostMapping(value = "/updateDescribe")
    @OperLog(operModule = "问题描述", operDesc = "修改问题描述", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult updateDescribe(@RequestBody ImIssueDescribeResponse imIssueDescribeResponse){
        Describe(imIssueDescribeResponse);
        if(imIssueDescribeResponse.getId()==0){
            throw new ApiException(ImRepositoryEnums.ISSUE_DESCRIBE_IS_NULL.getMessage());
        }
        return CommonResult.success(imIssueDesscribeService.updateDescribe(imIssueDescribeResponse));
    }

    @GetMapping(value = "/selectDescribe")
    @OperLog(operModule = "问题描述", operDesc = "按问题ID查询问题描述信息", operType = SysLogConstants.OPERATION_SELECT)
    public CommonResult selectDescribe(Integer id){
        if(EmptyUtil.isEmpty(id)){
            throw new ApiException(ImRepositoryEnums.ISSUE_DESCRIBE_IS_NULL.getMessage());
        }
        return CommonResult.success(imIssueDesscribeService.selectDescribe(id));
    }

    @PostMapping(value = "/updateDescribeStates")
    @OperLog(operModule = "问题描述", operDesc = "修改问题描述状态", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult updateDescribeStates(@RequestBody ImIssueDescribe imIssueDescribe){
        try {
            Assert.notNull(imIssueDescribe.getId(),"id不能为空");
            Assert.notNull(imIssueDescribe.getStates(),"states不能为空");
            imIssueDesscribeService.updateDescribeStates(imIssueDescribe);
        } catch (Exception e) {
            log.error("修改问题库是否推荐接口报错:{}",e.getMessage());
            return CommonResult.failed(e.getMessage());
        }
        return CommonResult.success();
    }
}
